package com.motherchild.admin.service.impl;

import com.motherchild.admin.dto.ProductDTO;
import com.motherchild.admin.mapper.ProductMapper;
import com.motherchild.admin.service.ProductService;
import com.motherchild.admin.vo.CategoryVO;
import com.motherchild.admin.vo.PageVO;
import com.motherchild.admin.vo.ProductDetailVO;
import com.motherchild.common.exception.BusinessException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.UUID;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public PageVO getList(Integer page, Integer pageSize, String keyword) {
        List<?> list = null;
        int total = 0;
        try {
            int offset = (page - 1) * pageSize;
            list = productMapper.selectList(offset, pageSize, keyword);
            total = productMapper.selectCount(keyword);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new PageVO(list, total);
    }

    @Override
    public ProductDetailVO getDetail(Integer id) {
        ProductDetailVO product = productMapper.selectDetail(id);
        if (product == null) {
            throw new BusinessException("商品不存在");
        }
        return product;
    }

    @Override
    @Transactional
    public void add(ProductDTO productDTO) {
        try {
            // 生成随机文件名
            String randomFileName = UUID.randomUUID().toString() + ".jpg";
            productDTO.setMainImage(randomFileName);

            productMapper.insert(productDTO);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    @Transactional
    public void update(Integer id, ProductDTO productDTO) {
        ProductDetailVO product = productMapper.selectDetail(id);
        if (product == null) {
            throw new BusinessException("商品不存在");
        }
        productDTO.setId(id);
        productMapper.update(productDTO);
    }

    @Override
    @Transactional
    public void delete(Integer id) {
        ProductDetailVO product = productMapper.selectDetail(id);
        if (product == null) {
            throw new BusinessException("商品不存在");
        }
        productMapper.delete(id);
    }

    @Override
    @Transactional
    public void updateStatus(Integer id, Integer status) {
        ProductDetailVO product = productMapper.selectDetail(id);
        if (product == null) {
            throw new BusinessException("商品不存在");
        }
        productMapper.updateStatus(id, status);
    }

    @Override
    @Transactional
    public void updateStock(Integer id, Integer stock) {
        ProductDetailVO product = productMapper.selectDetail(id);
        if (product == null) {
            throw new BusinessException("商品不存在");
        }
        if (stock < 0) {
            throw new BusinessException("库存不能小于0");
        }
        productMapper.updateStock(id, stock);
    }

    @Override
    public List<CategoryVO> getCategories() {
        return productMapper.selectCategories();
    }

    @Override
    public String getImageById(Integer productId) {
        return productMapper.getImageById(productId);
    }
}